﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.KhoHang
{
    public partial class frmQuanLy : Form
    {
        public frmQuanLy()
        {
            InitializeComponent();
        }
          private Entities.KhoHang[] ListKhoHang;
        private Entities.KhoHang KhoHang = null;
        private string MaKhoHang = string.Empty;
        private Boolean CheckClose = false;
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            if (capnhat == true)
            {
                if (GUI.NhanVien.frmXuLy.CheckClose == true)
                {
                    Them();
                }
                GUI.NhanVien.frmXuLy.CheckClose = false;
                HienThi();
            }
        }
        #endregion

        #region Check Permission
        //public bool CheckPer(int hanhDong)
        //{
        //    return Common.Utilities.Check(Common.FormName.QuanLyKhoHang, hanhDong);
        //}
        #endregion

        public void Them()
        {
            try
            {
                GUI.KhoHang.frmXuLy frm = new GUI.KhoHang.frmXuLy("Them", this.KhoHang);
                frm.GetData = new GUI.KhoHang.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception)
            { }
        }
        public void Sua()
        {
            try
            {
                if (this.KhoHang == null)
                {
                    MessageBox.Show("Bạn phải chọn kho hàng.", "Thông báo");
                    return;
                }
                GUI.KhoHang.frmXuLy frm = new GUI.KhoHang.frmXuLy("Sua", this.KhoHang);
                frm.GetData = new GUI.KhoHang.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        public void Xoa()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn xóa kho hàng này không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        Entities.KhoHang nv = new Entities.KhoHang();
                        nv.HanhDong = "Update";
                        nv.MaKho = this.MaKhoHang;
                        int k = Common.StaticVariable.InterfaceObject.DeletedKhoHang(nv);
                        if (k != 0)
                        {
                            this.HienThi();
                        }
                    }
                }
            }
            catch (Exception)
            { }
        }
        public void Form_Load()
        {
            HienThi();
        }
        public void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }

        public void HienThi()
        {

            try
            {
                Entities.KhoHang nv = new Entities.KhoHang();
                nv.HanhDong = "1";
                nv.MaKho = "";
                ListKhoHang = Common.StaticVariable.InterfaceObject.SelectKhoHang(nv);
                dgvHienThi.DataSource = ListKhoHang;
                //hien thi lai cai gidview
                this.HienThiListview();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                ListKhoHang = null;
                HienThiListview();
            }
        }

        public void HienThiListview()
        {
            try
            {
                if (ListKhoHang == null)
                {
                    Entities.KhoHang[] hh = new Entities.KhoHang[0];
                    dgvHienThi.DataSource = hh;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["HanhDong"].HeaderText = "STT";
                dgvHienThi.Columns["HanhDong"].Visible = true;
                dgvHienThi.Columns["MaKho"].HeaderText = "Mã Kho Hàng";
                dgvHienThi.Columns["MaKho"].Visible = true;
                dgvHienThi.Columns["TenKho"].HeaderText = "Tên Kho";
                dgvHienThi.Columns["TenKho"].Visible = true;
                dgvHienThi.Columns["MaNhanVien"].HeaderText = "Mã nhân viên";
                dgvHienThi.Columns["MaNhanVien"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        #region Processing
        //lay ban ghi de sua hoac xem
        private void GetRow()
        {
            try
            {
                if (ListKhoHang == null) { return; }
                for (int i = 0; i < this.ListKhoHang.Length; i++)
                {
                    if (ListKhoHang[i].MaKho == this.MaKhoHang)
                    {
                        KhoHang = ListKhoHang[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion
        #region Tìm
        public void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.KhoHang[])ListKhoHang.Where(p => p.MaKho.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            HienThiListview();
        }
        #endregion
        #region Sự kiện
        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgvHienThi.Rows.Count <= 0)
                { return; }
                MaKhoHang = dgvHienThi.CurrentRow.Cells["MaKho"].Value.ToString();
                this.GetRow();
            }
            catch (Exception)
            {
            }
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Sua();
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            Them();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Sua();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Xoa();
        }

        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            Form_Load();
            this.WindowState = FormWindowState.Maximized;
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }
        private void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (CheckClose == false)
                {
                    System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (ketqua == System.Windows.Forms.DialogResult.Yes)
                        {
                            e.Cancel = false;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
                GetData(true);
            }
            catch (Exception)
            { }
        }
        #endregion              

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

    }
}
